<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <title>登录注册</title>
</head>
<body>
  <form action="http://localhost:8080/regist" method="POST" class="regist">
    <h1>Regist</h1>
    <label for="">账号:</label>
    <input name="user" type="text" placeholder="请输入账号" class="user">
    <br>
    <label for="">密码:</label>
    <input name="psw" type="password" placeholder="请输入密码" class="psw">
    <br>
    <label for="">重复密码:</label>
    <input name="pswa" type="password" placeholder="请重复密码" class="pswa">
    <br>
    <input type="button" value="注册" class="button">
  </form>
  <br>
  <hr>
  <br>
<!--   <form action="http://localhost:8080/login" method="GET" class="login">
    <h1>Login</h1>
    <label for="">账号:</label>
    <input name="user" type="text" placeholder="请输入账号" class="user">
    <br>
    <label for="">密码:</label>
    <input type="password" name="psw" placeholder="请输入密码" class="psw">
    <br>
    <input type="button" value="登录" class="button">
  </form> -->
</body>
<!-- 登录验证 -->
<!-- <script>
  const login = document.querySelector('.login')
  const userInp = login.querySelector('.user')
  const pswInp = login.querySelector('.psw')
  const btn = login.querySelector('.button')
 
  login.addEventListener('click',(e) => {
    e = e || window.event
    const target = e.target || e.srcElement
    const user = userInp.value.trim()
    const psw = pswInp.value
    if(target.className === 'button'){
      const xhr = new XMLHttpRequest()
      xhr.open('GET',`http://localhost:8080/login?user=${user}&psw=${psw}`)
      xhr.onload = () => {
      const res = JSON.parse(xhr.responseText)
      if(res.code == 1){
        window.location.href = './login.html'
      }
      else{
        window.location.href = './regist.html'
      }
      console.log(res);
  }
  xhr.send()
    }
  })
</script> -->

<!-- 注册验证 -->
<script>
  const regist = document.querySelector('.regist')
  const userInp = regist.querySelector('.user')
  const pswInp = regist.querySelector('.psw')
  const pswaInp = regist.querySelector('.pswa')
  const btn = regist.querySelector('.button')
  regist.addEventListener('click',(e) => {
    e = e || window.event
    const target = e.target || e.srcElement
    if(target.className === 'button'){
      const user = userInp.value
      const psw = pswInp.value
      const pswa = pswaInp.value
      if(psw !== pswa) return
      const data = JSON.stringify({user:user,psw:psw,pswa:pswa})
      const xhr = new XMLHttpRequest()
      xhr.open('POST','http://localhost:8080/regist')
      xhr.onload = () => {
        const res = JSON.parse(xhr.responseText)
        console.log(res);
    }
      xhr.setRequestHeader("Content-type", "application/json")
      xhr.send(data)
    }
  })
</script>
</html>